package com.biallen.datastructure.queue;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 循环队列 算法 注意 对大小取余操作
 */
public class CircleQueueDemo {
    public static void main(String[] args) {
        CircleQueue myQueue = new CircleQueue(4);
        char key =' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop){
            System.out.println("p(display): 显示队列");
            System.out.println("a(add): 增加队列");
            System.out.println("e(exit): 退出队列");
            System.out.println("h(head): 获取头队列");
            System.out.println("g(getQueue): 获取队列");
            key=scanner.next().charAt(0);
            switch (key){
                case 'p':
                    myQueue.display();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int value = scanner.nextInt();
                    try {
                        myQueue.addQueue(value);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        int queue = myQueue.getQueue();
                        System.out.println("取出的数据是:"+queue);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int head = myQueue.head();
                        System.out.println("取出的头部数据是:"+head);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop=false;
                    break;
            }
        }
    }
}

class  CircleQueue{

    private int front;//队列的第一个元素

    private int tail;//队尾的第一个元素

    private int maxSize;

    private int[] data;

    public CircleQueue(int maxSize) {
        this.maxSize =  maxSize;
        this.data= new int[maxSize];

    }

    public  boolean isEmpty(){
        return  tail == front;
    }

    public boolean isFull(){

        return (tail+1) % maxSize == front;
    }

    /**
     * 取余计算 a%b =  a-(a/b)*b
     * @param dn
     */
    public void  addQueue(int dn){
             if(isFull()){
                 throw  new RuntimeException("队列已满");
             }
             data[tail]=dn;
             tail= (tail+1) % maxSize;//此处主要是针对指针的循环利用
    }

    public int head(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return  data[front];
    }

    public  int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }

        int datum = data[front];
        front=(front+1)% maxSize;
        return datum;
    }

    public void display() {
        for (int i = front; i < front+size(); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, data[i % maxSize]);
        }
    }

    public int size(){

        return (tail + maxSize - front) % maxSize;
    }

    @Override
    public String toString() {
        return "circleQueue{" +
                "front=" + front +
                ", tail=" + tail +
                ", maxSize=" + maxSize +
                ", data=" + Arrays.toString(data) +
                '}';
    }
}
